package cn.xyy.common.domain.mvc;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 分页实体类
 * 
 * @author xyy
 *
 * @param <T>
 */
public class Page<T> {

	private int total; // 总页数

	private int pageSize; // 分页步长

	private int page; // 当前页

	private int records; // 总记录数

	private Collection<T> items; // 数据

	public Page(Collection<T> items, int records, int pageSize, int page) {
		this.items = (items == null ? new ArrayList<T>(0) : items);
		this.records = records;
		this.page = page;
		this.pageSize = pageSize;
		this.total = calcTotalPage();
	}

	private int calcTotalPage() {
		int t = getRecords();
		int p = getPageSize();
		if (t == 0 || p == 0)
			return 0;
		int r = t % p;
		int pages = (t - r) / p;
		if (r > 0)
			pages += 1;
		return pages;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRecords() {
		return records;
	}

	public void setRecords(int records) {
		this.records = records;
	}

	public Collection<T> getItems() {
		return items;
	}

	public void setItems(Collection<T> items) {
		this.items = items;
	}

}
